/**
 * 
 */
package here.android.herehelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.os.StatFs;
import android.provider.ContactsContract.CommonDataKinds.Phone;

/**
 * @author here
 * 
 */
public class herePhoneInfo
{
	private final static String		TAG							= hereSocketServer.class
																		.toString( );

	/** 获取库Phone表字段 **/
	private static final String[]	PHONES_PROJECTION			= new String[] {
			Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID };

	/** 联系人显示名称 **/
	private static final int		PHONES_DISPLAY_NAME_INDEX	= 0;

	/** 电话号码 **/
	private static final int		PHONES_NUMBER_INDEX			= 1;

	/** 头像ID **/
	private static final int		PHONES_PHOTO_ID_INDEX		= 2;

	/** 联系人的ID **/
	private static final int		PHONES_CONTACT_ID_INDEX		= 3;

	public herePhoneInfo()
	{
	}

	// 这个是手机内存的可用空间大小
	static public long getAvailableInternalMemorySize()
	{
		File path = Environment.getDataDirectory( );
		StatFs stat = new StatFs(path.getPath( ));
		long blockSize = stat.getBlockSize( );
		long availableBlocks = stat.getAvailableBlocks( );
		return availableBlocks * blockSize;
	}

	// 这个是手机内存的总空间大小
	static public long getTotalInternalMemorySize()
	{
		File path = Environment.getDataDirectory( );
		StatFs stat = new StatFs(path.getPath( ));
		long blockSize = stat.getBlockSize( );
		long totalBlocks = stat.getBlockCount( );
		return totalBlocks * blockSize;
	}

	static public String getStrAvailableInternalMemorySize()
	{
		return formatSize(getAvailableInternalMemorySize( ));
	}

	static public String getStrTotalMemorySize()
	{
		return formatSize(getTotalMemory( ));
	}

	static public String formatSize(long size)
	{
		String suffix = null;

		if ( size >= 1024 )
		{
			suffix = "KiB";
			size /= 1024;
			if ( size >= 1024 )
			{
				suffix = "MiB";
				size /= 1024;
			}
		}

		StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

		int commaOffset = resultBuffer.length( ) - 3;
		while ( commaOffset > 0 )
		{
			resultBuffer.insert(commaOffset, ',');
			commaOffset -= 3;
		}

		if ( suffix != null )
			resultBuffer.append(suffix);
		return resultBuffer.toString( );
	}

	private static String formatFileSize(long filesize)
	{
		DecimalFormat df = new DecimalFormat("#.##");
		if ( filesize >= 1024d * 1024d * 0.8d )
			return df.format(filesize / (1024d * 1024d)) + "M";
		else if ( filesize >= 1024d * 0.8d )
			return df.format(filesize / 1024d) + "K";
		else if ( filesize == 0 )
			return df.format(filesize) + "Byte";
		return df.format(filesize) + "Bytes";
	}

	/*
	 * 获取内存总大小
	 */
	static public long getTotalMemory()
	{

		String file_path = "/proc/meminfo";// 系统内存信息文件
		String ram_info;
		String[] arrayOfRam;
		long initial_memory = 0;
		try
		{
			FileReader fr = new FileReader(file_path);
			BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
			// 读取meminfo第一行，系统总内存大小
			ram_info = localBufferedReader.readLine( );
			arrayOfRam = ram_info.split("\\s+");// 实现多个空格切割的效果
			initial_memory = Integer.valueOf(arrayOfRam[1]).intValue( ) * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte
			localBufferedReader.close( );
		}
		catch ( IOException e )
		{
			e.printStackTrace( );
		}

		return initial_memory;
	}

	static public String getContactOne(Context ctx)
	{
		try
		{
			ContentResolver resolver = ctx.getContentResolver( );

			Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
					PHONES_PROJECTION, null, null, null);
			if ( phoneCursor != null )
			{
				phoneCursor.moveToNext( );

				String contactName = phoneCursor
						.getString(PHONES_DISPLAY_NAME_INDEX);
				String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);

				return contactName + ": " + phoneNumber;
			}
		}
		catch ( Exception e )
		{
		}

		return "Get Failed";
	}

}
